import { Component, OnInit } from '@angular/core';
import {  ActivatedRoute } from '@angular/router';
import {RollService} from "../../../../../my-service/roll.service";
import {  NzMessageService } from 'ng-zorro-antd';
import {UtilService} from "../../../../../my-service/util.service";
declare var $: any;

@Component({
  selector: 'app-roll-notice',
  templateUrl: './roll-notice.component.html',
  styleUrls: ['./roll-notice.component.css'],
})
export class RollNoticeComponent implements OnInit {
  rollNotice={
    _this:null,//上下文执行环境
    rollGuid:null,//班级GUid
    currentStatus: '已发布',
    isNow:true,//已发布是否处于
    isDelete: false,//已删除是否处于
    isEditor:false,//是否处于已编辑状态
    sendStuArr:[],
    showSendStus:false,//是否展示发送学生
    showAll:false,//当文本内容多余6行时显示展开
    maxRow:6,//文本内容最多几行
    isAddNotice:false,
    nowNoticeArr:[],
    deleteNoticeArr:[],
    updateIndex:null,
    contentArr:null,//存放通知正文部分的文本节点；
    contentHeight:125,//正文部分默认显示高度
    defaultImg:'/assets/img/rollIndex/touxiang.png',//用户默认头像
    selectStatus:function (type) {
      // this.currentStatus = type;
      if(type === '回收站'){
          this._this.getDeleteNoticeList();
        this.isDelete = true;
        this.isNow = false;
        return;
      }else {
        this.isNow = true;
        this.isDelete = false;
        this._this.getNoticeList(0);
      }
    },//选择的是已删除还是已发布
    onCloseAddNotice:function (event) {
      this.isAddNotice = event;
     this._this.getNoticeList(0);
    },
    isShowMore:function () {
      for (let i = 0; i < this.contentArr.length; i++) {
        this.nowNoticeArr[i].isShowMore1=false;
          if(this.contentArr[i].clientHeight<=this.contentHeight){
            this.nowNoticeArr[i].isShowMore = false;
          }else {
            this.nowNoticeArr[i].isShowMore = true;
          }
      }
    },
    deleteIsShowMore:function () {
      for (let i = 0; i < this.contentArr.length; i++) {
        this.deleteNoticeArr[i].isShowMore1=false;
        if(this.contentArr[i].clientHeight<=this.contentHeight){
          this.deleteNoticeArr[i].isShowMore = false;
        }else {
          this.deleteNoticeArr[i].isShowMore = true;
        }
      }
    }
  };
  getNoticeList(state):any {
    this.rollServe.getNoticeList(this.rollNotice.rollGuid,state).then(res=>{
      if (res.status == 1){
        this.rollNotice.nowNoticeArr =res.data;
         setTimeout( () => {
           this.rollNotice.contentArr = document.getElementsByClassName('notice_content');
        this.rollNotice.isShowMore();
        }, 20);
        //延迟执行获得dom元素
      }else {
        console.log(res);
        this.msg.error('服务器内部错误，请刷新重试');
      }
    })
  }//获取未删除的通知
 constructor(
   private routerInfo:ActivatedRoute,
   private rollServe:RollService,
   private msg:NzMessageService,
   private utilServe:UtilService
   ){
    this.rollNotice._this = this;
 }
 getNoticeStudentList(notice) {
   notice.showSendStus=!notice.showSendStus;
     let noticeGuid = notice.PK_NoticeGuid;
     this.rollServe.getNoticeStudentList(noticeGuid).then(res=>{
       if(res.status == 1){
         notice.PublicObject = res.data;
         return res.data.length;
       }else {
         console.log(res);
         this.msg.error('服务器内部错误，请刷新重试');
       }
     })

 }
  ngOnInit() {
   this.rollNotice.rollGuid = this.routerInfo.snapshot.params['id'];
    this.getNoticeList(0);
  }
  getDeleteNoticeList() {
    this.rollServe.getNoticeList(this.rollNotice.rollGuid,99).then(res=>{
      if (res.status == 1){
        this.rollNotice.deleteNoticeArr=res.data;
        setTimeout( () => {
          this.rollNotice.contentArr = document.getElementsByClassName('notice_content');
          this.rollNotice.deleteIsShowMore();
        }, 0);
        //延迟执行获得dom元素
      }else {
        console.log(res);
        this.msg.error('服务器内部错误，请刷新重试');
      }
    })
  }
 deleteNotice(noticeGuid,index) {
     this.utilServe.comfirm('您确认删除该公告吗',()=>{
        this.rollServe.deleteNotice(noticeGuid).then(res=>{
          if(res.status == 1){
           this.rollNotice.nowNoticeArr.splice(index,1);
            this.msg.success('删除成功');
          }else {
            console.log(res);
            this.msg.error('服务器内部错误，请刷新重试')
          }
        })
     })
 }
 recoverNotice(noticeGuid,index) {
     this.utilServe.comfirm('您确认恢复该公告吗',()=>{
       this.rollServe.recoverNotice(noticeGuid).then(res=>{
         if(res.status == 1){
            this.rollNotice.deleteNoticeArr.splice(index,1);
           this.msg.success('恢复成功');
         }else {
           console.log(res);
           this.msg.error('服务器内部错误，请刷新重试');
         }
       })
   })
 };
  updateNotice(index){
    this.utilServe.comfirm('您确认修改该公告吗',()=>{
      this.rollNotice.isAddNotice = true;
      this.rollNotice.updateIndex = index;
    });
  }
}
